package com.css.zfzx.sjcj.modules.hddcwyGeophysvypoint.repository.impl;

import com.css.zfzx.sjcj.modules.hddcwyGeophysvypoint.repository.HddcWyGeophysvypointNativeRepository;
import com.css.zfzx.sjcj.modules.hddcwyGeophysvypoint.repository.entity.HddcWyGeophysvypointEntity;
import com.css.zfzx.sjcj.modules.hddcwyGeophysvypoint.viewobjects.HddcWyGeophysvypointQueryParams;
import com.css.bpm.platform.utils.PlatformObjectUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.math.BigInteger;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
/**
 * @author zyb
 * @date 2020-12-01
 */
@Repository
public class HddcWyGeophysvypointNativeRepositoryImpl implements HddcWyGeophysvypointNativeRepository {
    @PersistenceContext
    private EntityManager em;

    private static final Logger log = LoggerFactory.getLogger(HddcWyGeophysvypointNativeRepositoryImpl.class);


    @Override
    public Page<HddcWyGeophysvypointEntity> queryHddcWyGeophysvypoints(HddcWyGeophysvypointQueryParams queryParams ,
                                                                       int curPage, int pageSize) {
        StringBuilder sql = new StringBuilder("select * from hddc_wy_geophysvypoint ");
        StringBuilder whereSql = new StringBuilder(" where 1=1 ");
        if(!PlatformObjectUtils.isEmpty(queryParams.getProvince())) {
            whereSql.append(" and province = :province");
        }
        if(!PlatformObjectUtils.isEmpty(queryParams.getCity())) {
            whereSql.append(" and city = :city");
        }
        if(!PlatformObjectUtils.isEmpty(queryParams.getArea())) {
            whereSql.append(" and area = :area");
        }
        if(!PlatformObjectUtils.isEmpty(queryParams.getProjectName())) {
            whereSql.append(" and project_name like :projectName");
        }
        if(!PlatformObjectUtils.isEmpty(queryParams.getLabelinfo())) {
            whereSql.append(" and labelinfo like :labelinfo");
        }
        Query query = this.em.createNativeQuery(sql.append(whereSql).toString(), HddcWyGeophysvypointEntity.class);
        StringBuilder countSql = new StringBuilder("select count(*) from hddc_wy_geophysvypoint ");
        Query countQuery = this.em.createNativeQuery(countSql.append(whereSql).toString());
        if(!PlatformObjectUtils.isEmpty(queryParams.getProvince())) {
            query.setParameter("province", queryParams.getProvince());
            countQuery.setParameter("province", queryParams.getProvince());
        }
        if(!PlatformObjectUtils.isEmpty(queryParams.getCity())) {
            query.setParameter("city", queryParams.getCity());
            countQuery.setParameter("city", queryParams.getCity());
        }
        if(!PlatformObjectUtils.isEmpty(queryParams.getArea())) {
            query.setParameter("area", queryParams.getArea());
            countQuery.setParameter("area", queryParams.getArea());
        }
        if(!PlatformObjectUtils.isEmpty(queryParams.getProjectName())) {
            query.setParameter("projectName", "%" + queryParams.getProjectName() + "%");
            countQuery.setParameter("projectName", "%" + queryParams.getProjectName() + "%");
        }
        if(!PlatformObjectUtils.isEmpty(queryParams.getLabelinfo())) {
            query.setParameter("labelinfo", "%" + queryParams.getLabelinfo() + "%");
            countQuery.setParameter("labelinfo", "%" + queryParams.getLabelinfo() + "%");
        }
        Pageable pageable = PageRequest.of(curPage - 1, pageSize);
        query.setFirstResult(pageable.getPageNumber() * pageable.getPageSize());
        query.setMaxResults(pageable.getPageSize());
        List<HddcWyGeophysvypointEntity> list = query.getResultList();
        BigInteger count = (BigInteger) countQuery.getSingleResult();
        Page<HddcWyGeophysvypointEntity> hddcWyGeophysvypointPage = new PageImpl<>(list, pageable, count.longValue());
        return hddcWyGeophysvypointPage;
    }
}
